<?php
require 'Sabai/Form.php';
require 'Sabai/Form/Decorator/Token.php';
require 'Sabai/Form/Element/SelectDropdown.php';
require 'Sabai/Validator/Inclusion.php';

/**
 * Short description for class
 *
 * Long description for class (if any)...
 *
 * @category   SabaiXOOPS
 * @package    SabaiXOOPS
 * @copyright  Copyright (c) 2008 myWeb Japan (http://www.myweb.ne.jp/)
 * @author     Kazumi Ono <onokazu@gmail.com>
 * @license    http://opensource.org/licenses/gpl-license.php GNU GPL
 * @link       http://sourceforge.net/projects/sabai
 * @since      Class available since Release 0.1.0
 */
class SabaiXOOPS_GroupPermission
{
    var $_appRoles;
    var $_xoopsModule;
    var $_templateName = 'RoleAdministration';
    var $_formVar = 'form';
    var $_returnURL = SABAI_URL;
    var $_successMsg = 'Roles updated successfully';
    var $_errorMsg = 'Failed to delete roles for this module';

    function SabaiXOOPS_GroupPermission($appRoles, &$module)
    {
        $this->_appRoles = $appRoles;
        $this->_xoopsModule =& $module;
    }

    function setTemplateName($templateName)
    {
        $this->_templateName = $templateName;
    }

    function setFormVar($formVar)
    {
        $this->_formVar = $formVar;
    }

    function setSuccessURL($returnURL)
    {
        $this->_successURL = $returnURL;
    }

    function setSuccessMsg($successMsg)
    {
        $this->_successMsg = $successMsg;
    }

    function setErrorMsg($errorMsg)
    {
        $this->_errorMsg = $errorMsg;
    }

    function process(&$request, &$response, &$user)
    {
        $module_id = $this->_xoopsModule->getVar('mid');
        $perm_name = $this->_xoopsModule->getVar('dirname') . '_role';
        $form =& $this->_getForm($module_id, $perm_name);
        if ($request->isPost()) {
            if ($form->validateValues($request->getAll())) {
                $groupperm_h =& xoops_gethandler('groupperm');
                if ($groupperm_h->deleteByModule($module_id, $perm_name)) {
                    foreach ($request->getAsArray('roles') as $group_id => $role_ids) {
                        if (($group_id == XOOPS_GROUP_ANONYMOUS) || $groupperm_h->checkRight('module_admin', $module_id, $group_id)) {
                            continue;
                        }
                        foreach ($role_ids as $role_id) {
                            $groupperm_h->addRight($perm_name, $role_id, $group_id, $module_id);
                        }
                    }
                    $response->setSuccess($this->_successMsg, $this->_successURL);
                    $response->send($request);
                    exit();
                } else {
                    $form->addFormValidationError($this->_errorMsg);
                }
            }
        }
        $form->onView();
        $response->pushContentName($this->_templateName);
        $response->setVarRef($this->_formVar, $form);
        $response->send($request);
    }

    function &_getForm($moduleId, $permName)
    {
        $form =& new Sabai_Form_Decorator_Token(new Sabai_Form(), 'XOOPS_RoleAdmin');
        $member_h =& xoops_gethandler('member');
        $groupperm_h =& xoops_gethandler('groupperm');
        foreach ($member_h->getGroupList() as $group_id => $group_name) {
            if ($group_id == XOOPS_GROUP_ANONYMOUS) {
                continue;
            }
            $element =& new Sabai_Form_Element_SelectDropdown(sprintf('roles[%d]', $group_id), 5,  true);
            foreach ($this->_appRoles as $role_id => $role_name) {
                $element->addOption($role_id, $role_name);
            }
            if ($groupperm_h->checkRight('module_admin', $moduleId, $group_id)) {
                $element->setValue(array_keys($this->_appRoles));
            } else {
                $element->setValue($groupperm_h->getItemIds($permName, $group_id, $moduleId));
            }
            // make sure selected group ids are valid ones
            $element->addValidator(new Sabai_Validator_Inclusion(array_keys($this->_appRoles)), 'Invalid role id');
            $form->addElement($element, $group_name);
        }
        return $form;
    }
}